/*******************************************************************************
 * This file is part of blueprint4j www.blueprint4j.org.
 * 
 *     blueprint4j is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU Lesser General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 * 
 *     blueprint4j is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU Lesser General Public License for more details.
 * 
 *     You should have received a copy of the GNU Lesser General Public License
 *     along with blueprint4j. If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/
package blueprint4j.utils;

import java.io.*;
import java.util.*;
import java.math.*;

public class LogOld {
	static Vector logs = new Vector();

	// Can not be constructed
	private LogOld() {}

	static public void addLogging(Logging log) {
		logs.add(log);
	}

	synchronized static public void writeLog(Logging.Level lvl, Throwable details) {
		ByteArrayOutputStream buffer = new ByteArrayOutputStream();
		details.printStackTrace(new PrintWriter(buffer, true));
		writeLog(lvl, details.getMessage() , new String(buffer.toByteArray()));
	}

	synchronized static public void writeLog(Logging.Level lvl, String description, String details) {
		for (int t = 0;t < logs.size();t++) {
			((Logging)logs.get(t)).writeLog(lvl, ThreadId.getCurrentId() , description, details);
		}
		if (lvl.toString().equals(Logging.CRITICAL.toString())) {
			System.exit(-1);
		}
	}

	synchronized static public void writeLog(Logging.Level lvl, String description, Throwable throwable) {
		for (int t = 0;t < logs.size();t++) {
			((Logging)logs.get(t)).writeLog(lvl, ThreadId.getCurrentId() , description, throwable);
		}
	}

	synchronized static public LogDataUnitVector findLogs(Integer tid,java.util.Date from_date,java.util.Date to_date,Logging.Level levels[])
	throws IOException {
		LogDataUnitVector log_data_units = new LogDataUnitVector();
		int size = 0;
		for (int t=0;t<logs.size();t++){
			LogDataUnitVector v_log_data_units = ((Logging)logs.get(t)).findLogs(tid,from_date,to_date,levels);
			log_data_units.add(v_log_data_units);
		}
		return log_data_units;
	}

	synchronized static public void writeLog(Logging.Level lvl, String description, Properties props) {
		String propstr = "";
		try {
			ByteArrayOutputStream ba = new ByteArrayOutputStream();
			props.store(ba, "");
			propstr = ba.toString();
		} catch (IOException e) {}
		LogOld.writeLog(lvl, description, propstr);
	}

	synchronized static public void writeLog(Logging.Level lvl, String description, byte[] data) {
		//String strdata = "";
		StringBuffer strdata = new StringBuffer();
		StringBuffer pos = new StringBuffer();
		StringBuffer hex = new StringBuffer();
		StringBuffer str = new StringBuffer();
		for (int b = 0;b < data.length;b += 16) {
			//String pos = new BigInteger("" + b).toString(16);
			//String hex = "";
			String one;
			//String str = "";
			pos.delete(0,pos.length());
			hex.delete(0,hex.length());
			str.delete(0,str.length());
			while (pos.length() < 4) pos.insert(0,"0");
			for (int u = 0;u < 16;u++) {
				if (b + u < data.length) {
					int value = data[b + u];
					if (value < 0) value += 128;
					one = new BigInteger("" + value).toString(16);
					while (one.length() < 2) one = "0" + one;
					//hex = hex + one;
					hex.append(one);
					if (u == 7) {
						//hex = hex + "-";
						hex.append("-");
					} else {
						//hex = hex + " ";
						hex.append(" ");
					}
					if (value >= 32 && value <= 127) {
						str.append(new String(new byte[]{(byte)value}));
						//str = str + new String(new byte[]{(byte)value});
					} else {
						//str = str + ".";
						str.append(".");
					}
				} else {
					//hex = hex + "   ";
					hex.append("   ");
					//str = str + " ";
					str.append(" ");
				}
			}
			//strdata = strdata + pos + ":  " + hex + "  " + str + "\r\n";
			strdata.append(pos + ":  " + hex + "  " + str + "\r\n");
		}
		for (int t = 0;t < logs.size();t++) {
			((Logging)logs.get(t)).writeLog(lvl, ThreadId.getCurrentId(), description, strdata.toString());
		}
	}

	static private Date next_cleanup = new Date();

	static boolean isTimeForNextCleanup() {
		return next_cleanup.compareTo(new Date()) <= 0;
	}

	static void doCleanUp() {
		// Work out time for next cleanout
		Calendar cal = Calendar.getInstance();
		cal.setTime(new Date());
		cal.add(Calendar.MINUTE, 15);
		next_cleanup = cal.getTime();
	}

	static public void close() {
		for (int t = 0;t < logs.size();t++) {
			((Logging)logs.get(t)).close();
		}
		logs = new Vector();
	}
}


